package com.shenw.frame.content;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ContentReader {

	private HashMap<String, ContentBean> contentMap;
	private boolean init = false;
	
	@SuppressWarnings("unchecked")
	private void init() throws Exception {
		contentMap = new HashMap<String, ContentBean>();
		Document doc = new SAXReader().read(new FileInputStream(new File("src/main/resources/content.xml")));
		Element root = doc.getRootElement();
		List<Element> elems = root.elements();
		ContentBean bean;
		for (Element elem : elems) {
			bean = new ContentBean();
			String input = elem.elementText("input");
			String output = elem.elementText("output");
			
			bean.in = input;
			bean.out = output;
			contentMap.put(elem.getName(), bean);
		}
	}
	
	public ContentBean get(String key) throws Exception {
		if (!init) {
			init();
		}
		return contentMap.get(key);
	}
	
	public class ContentBean {
		
		private String in;
		private String out;
		
		public String getIn() {
			return in;
		}
		public String getOut() {
			return out;
		}
	}
}
